//
//  CommonShowCollectionView.m
//  DoulinClient
//
//  Created by ypj on 2021/6/9.
//

#import "CommonShowCollectionView.h"
//#import "PingJiaImageCollectionViewCell.h"
@interface CommonShowCollectionView()
@property (nonatomic, assign) CGFloat itemW;
@property (nonatomic, assign) CGFloat itemH;

@end
@implementation CommonShowCollectionView
- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.itemW = floor((ScreenWidth-30.0)/4.0);
    self.itemH = floor((ScreenWidth-30.0)/4.0);
    self.collectionView.showsVerticalScrollIndicator=NO;
    self.collectionView.showsHorizontalScrollIndicator=NO;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(self.itemW, self.itemH);
    _collectionView.collectionViewLayout = layout;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
//    [_collectionView registerNib:kNib(PingJiaImageCollectionViewCell) forCellWithReuseIdentifier:kClsID(PingJiaImageCollectionViewCell)];
    [_collectionView registerClass:[UICollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

+ (instancetype)shareInstance{
    return [[NSBundle mainBundle] loadNibNamed:@"CommonShowCollectionView" owner:nil options:nil][0];
}

-(void)setDataImageArray:(NSMutableArray *)imageArray{
    self.imageArray = imageArray;
    [self getCollectionViewHeight];
    [self.collectionView reloadData];
}

#pragma mark----delegate、dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

//    PingJiaImageCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:kClsID(PingJiaImageCollectionViewCell) forIndexPath:indexPath];
    UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        UICollectionReusableView *headerV = (UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"header"forIndexPath:indexPath];
        return headerV;
        
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
        UICollectionReusableView  * footerView=(UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collettionSectionFoot" forIndexPath:indexPath];
        return footerView;
        
    } else {
        
        return nil;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  photoWidth=(ScreenWidth-30.0)/4.0;
    CGSize size=CGSizeMake(photoWidth, photoWidth);
    return size;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  
}

-(void)getCollectionViewHeight{
   CGFloat photoWidth = (ScreenWidth-30.0)/4.0;
    _collectionViewHeight = 0;
    _collectionViewHeight = _collectionViewHeight+10;
    NSInteger number = self.imageArray.count%4;
    NSInteger zomeNumber = self.imageArray.count/4;
    if (number == 0){
        number = zomeNumber;
    } else {
        number = zomeNumber+1;
    }
    _collectionViewHeight = _collectionViewHeight + (number * photoWidth);
    _collectionView.height = _collectionViewHeight;
    _bgView.height = _collectionViewHeight + 10;
     [_collectionView reloadData];
}
@end
